package cn.element.leetcode.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: gonghongwei
 * @create: 2021-11-29 16:43
 */
public class DeptTest {
	public static class Department {
		private Long id;
		private String name;
		private List<Department> children;

		public Department() {
		}

		public Department(Long id, String name, List<Department> children) {
			this.id = id;
			this.name = name;
			this.children = children;
		}

		public Long getId() {
			return id;
		}

		public void setId(Long id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public List<Department> getChildren() {
			return children;
		}

		public void setChildren(List<Department> children) {
			this.children = children;
		}
	}

	//递归获取部门id
	public static List<Long> getDepartmentIds(List<Department> departments, String departName, List<Long> departIds) {
		//1.结束递归条件
		if (departName == null) {
			return null;
		}
		if (departments == null) {
			return departIds;
		}


		for (int i = 0; i < departments.size(); i++) {
			Department department = departments.get(i);
			if (departName.equals(department.getName())) {
				departIds.add(department.getId());
			}

			if (department.getChildren() != null) {
				getDepartmentIds(department.getChildren(), departName, departIds);
			}
		}
		return departIds;
	}

	public static void main(String[] args) {

		List<Department> childrens = new ArrayList<>();
		List<Department> parents = new ArrayList<>();
		Department dp1 = new Department(1L, "zs", new ArrayList());
		Department dp2 = new Department(2L, "ls", new ArrayList<>());
		Department dp3 = new Department(3L, "wu", new ArrayList<>());
		Department dp4 = new Department(4L, "wu", new ArrayList<>());
		Department parent = new Department(5L,"wu",childrens);
		parents.add(parent);
		childrens.add(dp1);
		childrens.add(dp2);
		childrens.add(dp3);
		childrens.add(dp4);

		List<Long> wu = getDepartmentIds(parents, "wu", new ArrayList<>());
		System.out.println(Arrays.toString(wu.toArray()));

	}
}
